package cate.game.admin.menu.task;

import cate.common.table.d.GDFunc;
import cate.game.GameBody;
import cate.game.admin.menu.MenuSetup;
import cate.game.role.task.TaskGroup;
import cate.game.role.task.TaskItem;
import cate.game.role.task.msg.TaskGroupDataResp;
import cate.game.role.task.msg.TaskGroupUpdateResp;
import easy.java.practice.errorcode.EcResult;

public class TaskMenu extends MenuSetup {

    public TaskMenu(GameBody gameBody) {
        super("任务相关");

        reg(gameBody, "领取任务奖励", 2, "参数1:功能ID 参数2：任务ID",
                (role, params) -> {
                    if (params.size() < 2) {
                        gameBody.notice.message(role, "参数不能少于1个!");
                        return;
                    }
                    int funcId = Integer.parseInt(params.get(0));
                    int taskId = Integer.parseInt(params.get(1));
                    EcResult<TaskItem> r = role.getTask().getRewardDirect(funcId, taskId);
                    if (r.ok()) {
                        TaskItem item = r.data;
                        role.sendNow(new TaskGroupUpdateResp(item));
                    } else {
                        role.getGame().notice.message(role, r.message);
                    }

                });

        reg(gameBody, "赠送友情点次数", 1, "参数1:次数 ",
                (role, params) -> {
                    if (params.size() < 1) {
                        gameBody.notice.message(role, "参数不能少于1个!");
                        return;
                    }
                    int num = Integer.parseInt(params.get(0));
                    role.getFriend().testTask(num);
                });

      

        reg(gameBody, "重置主线任务(引导)", 0, "",
                (role, params) -> {
                    role.getTask().getGroup(GDFunc.MAINLINE).clear();
                    role.getTask().mainline.curTaskId = 0;
                    role.getTask().mainline.update();
                    role.getGame().notice.message(role, "更新成功");
                });

        reg(gameBody, "主线引导强制跳下一个", 0, "",
                (role, params) -> {
                    role.getTask().getGroup(GDFunc.MAINLINE).clear();
                    role.getTask().mainline.addNextTask();
                    role.getGame().notice.message(role, "更新成功,请重新登录");
                });

        reg(gameBody, "主线引导跳至任务ID", 1, "参数:任务id",
                (role, params) -> {
                    if (params.size() < 1) {
                        gameBody.notice.message(role, "参数不能少于1个!");
                        return;
                    }
                    role.getTask().getGroup(GDFunc.MAINLINE).clear();
                    int curTask = Integer.parseInt(params.get(0));
                    role.getTask().mainline.replaceCurTask(curTask);
                    role.getGame().notice.message(role, "更新成功,请重新登录");
                });

        reg(gameBody, "完成某个任务", 2,"参数1：任务组  参数2： 任务id",
                (role, params) -> {
                    if (params.size() < 2) {
                        gameBody.notice.message(role, "参数不能少于2个!");
                        return;
                    }
                    TaskGroup group = role.getTask().getGroup(Integer.parseInt(params.get(0)));
                    if(group == null){
                        gameBody.notice.message(role, "任务组不存在!");
                        return;
                    }
                    int curTask = Integer.parseInt(params.get(1));
                    TaskItem taskItem = group.getItem(curTask);
                    if(taskItem == null){
                        gameBody.notice.message(role, "任务不存在!");
                        return;
                    }
                    taskItem.done = true;
                    group.record(1,1,1,true);
                    role.getGame().notice.message(role, "任务已完成");
                });

        reg(gameBody, "完成某个任务组的任务", 1,"参数1：任务组",
                (role, params) -> {
                    if (params.size() < 1) {
                        gameBody.notice.message(role, "参数不能少于1个!");
                        return;
                    }
                    TaskGroup group = role.getTask().getGroup(Integer.parseInt(params.get(0)));
                    if(group == null){
                        gameBody.notice.message(role, "任务组不存在!");
                        return;
                    }
                    for (TaskItem taskItem : group.ingList) {
                        taskItem.value = taskItem.getTpl().value;
                        taskItem.done = true;
                    }
                    group.record(1,1,1,true);
                    //role.sendNow(new TaskDataResp(role));
                    role.sendNow(  new TaskGroupDataResp(group));
                    role.getGame().notice.message(role, "任务已完成");
                });
    }
}
